package com.hht.baselibs.layoutManager

import androidx.annotation.IntRange
import androidx.recyclerview.widget.RecyclerView


/**
 * Author:chen'hu
 * Time:2023/8/10 15:16
 * Description:RecycleView使用这个就够用了
 */
class LayoutManagerHelper {
    private var rows: Int = 1;
    private var columns: Int = 1;
    private var orientation: Int = RecyclerView.HORIZONTAL
    private lateinit var recycleViewLayoutManager: RecycleViewLayoutManager
    private var mScroll: Boolean = true
    private var mIndicatorView: IndicatorView? = null

    companion object {
        fun newInstance(): LayoutManagerHelper {
            return LayoutManagerHelper()
        }
    }

    fun setRows(@IntRange(from = 1, to = 100) rows: Int): LayoutManagerHelper {
        this.rows = rows
        return this
    }

    fun setColumns(@IntRange(from = 1, to = 100) columns: Int): LayoutManagerHelper {
        this.columns = columns
        return this
    }

    fun setOrientation(@RecycleViewLayoutManager.OrientationType orientation: Int): LayoutManagerHelper {
        this.orientation = orientation
        return this
    }

    /**
     * 上一页
     */
    fun getPrePage(): LayoutManagerHelper {
        recycleViewLayoutManager.getPrePage()
        return this
    }

    /**
     * 下一页
     */
    fun getNextPage(): LayoutManagerHelper {
        recycleViewLayoutManager.getNextPage()
        return this
    }

    /**
     * 获取当前页
     */
    fun getCurrentPage(): Int {
        return recycleViewLayoutManager.getCurrentPage()
    }

    /**
     * 指定到页
     * */
    fun setCurrentPage(index: Int) {
        recycleViewLayoutManager.smoothScrollToPage(index)
    }

    /**
     * 总页数
     * */
    fun getTotalNumberPages(): Int {
        return recycleViewLayoutManager.getTotalNumberPages()
    }

    fun onPageSelected(pageListener: RecycleViewLayoutManager.PageListener) {
        recycleViewLayoutManager.setPageListener(pageListener)
    }

    fun getRecycleViewLayoutManager(): RecycleViewLayoutManager {
        return recycleViewLayoutManager
    }


    fun bindRecycleView(recyclerView: RecyclerView) {
        bindRecycleView(recyclerView, mIndicatorView, mScroll)
    }

    fun bindRecycleView(
        recyclerView: RecyclerView,
        indicator: IndicatorView? = null,
    ) {
        mIndicatorView = indicator
        bindRecycleView(recyclerView, mIndicatorView, mScroll)
    }

    fun bindRecycleView(
        recyclerView: RecyclerView,
        indicator: IndicatorView? = null,
        scroll: Boolean = false,
    ) {
        val manager = getLayoutManager()
        //是否可以连续滑动
        manager.setAllowContinuousScroll(scroll)
        recyclerView.layoutManager = manager
        recyclerView.onFlingListener = null
        getRecycleViewSnapHelper().attachToRecyclerView(recyclerView)
        indicator?.setLayoutManagerPageListener(manager)
    }


    private fun getLayoutManager(): RecycleViewLayoutManager {
        recycleViewLayoutManager = RecycleViewLayoutManager(rows, columns, orientation)
        return recycleViewLayoutManager
    }

    private fun getRecycleViewSnapHelper(): RecycleViewSnapHelper {
        return RecycleViewSnapHelper()
    }


}